<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>单例模式-书籍-静态方法</title>
</head>
<body>
    <script>
        class single {
            constructor(name) {
                this.name = name

            }
            getName() {
                console.log(this.name)
            }
            /* static getInstance(name) {
                if(!this.instance) {
                    this.instance = new single(name)
                }
                return this.instance
                
            } 
            static instance = null
            */
            static getInstance = (function() {
                let instance = null 
                return function(name) {
                    if(!instance) {
                        instance = new single(name)
                    }
                    return instance
                }
            })()
        }

        let a = single.getInstance('a')
        let b = single.getInstance('b')
        console.log(a === b);

    </script>
</body>
</html>